﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI.WebControls;

namespace VitCode.Web.BackStageManagement
{
    public partial class UserUpdate : VcPage
    {

        
        
        BaseDataManager m_Help = new BaseDataManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (!IsPostBack)
            {
                DropDownList_UserGroup.Items.Add(new ListItem("", ""));
                DropDownList_Role.Items.Add(new ListItem("", ""));

                string sql = "select Id,Name from VitCode_Castle_UserGroup";
                DataTable dt = m_Help.GetDataTableBySql(sql);

                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        DropDownList_UserGroup.DataSource = dt;
                        DropDownList_UserGroup.DataTextField = "Name";
                        DropDownList_UserGroup.DataValueField = "Id";
                        DropDownList_UserGroup.DataBind();
                        DropDownList_UserGroup.Items.Add(new ListItem("", ""));
                        dt.Dispose();
                    }
                }

                sql = "select Id,Name from VitCode_Castle_Role";
                DataTable dt2 = m_Help.GetDataTableBySql(sql);
                if (dt2 != null)
                {
                    if (dt2.Rows.Count > 0)
                    {
                        DropDownList_Role.DataSource = dt2;
                        DropDownList_Role.DataTextField = "Name";
                        DropDownList_Role.DataValueField = "Id";
                        DropDownList_Role.DataBind();
                        DropDownList_Role.Items.Add(new ListItem("", ""));
                        dt2.Dispose();
                    }
                }

                if (!String.IsNullOrEmpty(GenericContext.CurrUserCode))
                {
                    if (Request.QueryString["Id"] != null)
                    {
                        lableId.Text = Request.QueryString["Id"].Trim();

                        User currentUser;

                        IList objlist = m_Help.GetObjectList(new User().GetType().Name, "Id", lableId.Text.Trim());
                        if (objlist != null && objlist.Count > 0)
                        {
                            currentUser = (User)objlist[0];

                            ZLTextBox_Code.Text = currentUser.Code;
                            ZLTextBox_Number.Text = currentUser.Number;
                            ZLTextBox_Mobile.Text = currentUser.Mobile;
                            ZLTextBox_Email.Text = currentUser.Email;
                            ZLTextBox_AdminRealName.Text = currentUser.Name;
                            if(currentUser.UserGroup != null)
                            {
                                string id = currentUser.UserGroup.Id.ToString().Trim();
                                if (DropDownList_UserGroup.Items.Count > 0 && DropDownList_UserGroup.Items.FindByValue(id) != null)
                                {
                                    DropDownList_UserGroup.Items.FindByValue(id).Selected = true;
                                }
                              
                            }
                            else
                            {
                                DropDownList_UserGroup.Items.FindByValue("").Selected = true;
                            }
                         
                            IList objList2 = m_Help.GetObjectList(new UserRoleRelation().GetType().Name, "UserId",
                                                                  lableId.Text);
                            if(objList2 != null && objList2.Count > 0)
                            {
                                var currentRoleRelation = (UserRoleRelation)objList2[0];
                                if(currentRoleRelation.RoleId.ToString().Length == 36)
                                {
                                    Guid guidRoleId = currentRoleRelation.RoleId;
                                    if (DropDownList_Role.Items.Count > 0 && DropDownList_Role.Items.FindByValue(guidRoleId.ToString()) != null)
                                    {
                                        DropDownList_Role.Items.FindByValue(guidRoleId.ToString()).Selected = true;
                                    }
                                   
                                }
                            }
                            else
                            {
                                DropDownList_Role.Items.FindByValue("").Selected = true;
                            }
                         
                           
                        }
                    }
                }
                else
                {
                    Response.Redirect("../Default.aspx");
                }
            }

        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {

            Response.Redirect("UserList.aspx", false);
        }

        protected void Button_Update_Click(object sender, EventArgs e)
        {
            bool access;
            string strCode = ZLTextBox_Code.Text;
            string strNumber = ZLTextBox_Number.Text;
            string strMobile = ZLTextBox_Mobile.Text;
            string strEmail = ZLTextBox_Email.Text;
            string strName = ZLTextBox_AdminRealName.Text;

            string hql = " from User where Code ='" + ZLTextBox_Code.Text.Trim() + "' and Id !='" +
                         lableId.Text.Trim() + "'";
            IList userList = m_Help.Session.CreateQuery(hql).List();
            if (userList != null && userList.Count > 0)
            {
                Alert.MessageBox("用戶編號已存在！");
                return;
            }
            User currentUser;
            IList objlist = m_Help.GetObjectList(new User().GetType().Name, "Id", lableId.Text.Trim());
            if (objlist != null && objlist.Count > 0)
            {
                currentUser = (User)objlist[0];
                currentUser.Name = strName;
                currentUser.Mobile = strMobile;
                currentUser.Email = strEmail;
                currentUser.Number = strNumber;
                currentUser.Code = strCode;

                if (DropDownList_UserGroup.SelectedItem.Text.Trim().Length > 0)
                {
                    string strUserGroup = DropDownList_UserGroup.SelectedItem.Value.Trim();

                    IList objList = m_Help.GetObjectList(new VitCode.Castle.Entities.UserGroup().GetType().Name, "Id",
                                                      strUserGroup);
                    if (objList != null && objList.Count > 0)
                    {
                        var currentUserGroup =
                            (VitCode.Castle.Entities.UserGroup)objList[0];
                        currentUser.UserGroup = currentUserGroup;

                    }
                }
                if (DropDownList_Role.SelectedItem.Text.Trim().Length > 0)
                {
                       Guid guidRoleId = new Guid(DropDownList_Role.SelectedItem.Value.Trim());
                       IList objList = m_Help.GetObjectList(new UserRoleRelation().GetType().Name, "UserId", lableId.Text);
                       if (objList != null && objList.Count > 0)
                       {
                           var currentRoleRelation = (UserRoleRelation) objList[0];
                           currentRoleRelation.RoleId = guidRoleId;
                           access = m_Help.SaveOrUpdateObj(currentRoleRelation);
                       }else
                       {
                           var currentRoleRelation = new UserRoleRelation
                                                         {
                                                             RoleId = guidRoleId,
                                                             UserId = new Guid(lableId.Text.Trim())
                                                         };
                           access = m_Help.SaveOrUpdateObj(currentRoleRelation);
                       }
                }
                else
                {
                    IList objList = m_Help.GetObjectList(new UserRoleRelation().GetType().Name, "UserId", lableId.Text);
                    if (objList != null && objList.Count > 0)
                    {
                        var currentRoleRelation = (UserRoleRelation)objList[0];
                        m_Help.DeleteObj(currentRoleRelation);
                    }
                }
                access = m_Help.SaveOrUpdateObj(currentUser);
                if (access)
                    Page.RegisterClientScriptBlock("資訊提示", "<script language='javascript'>alert('保存成功！')</script>");
            }
        }
        protected void Select_Click(object sender, EventArgs e)
        {
            Window win = new Window(Page, "角色選擇");
            win.Height = 400;
            win.Width = 600;
            win.Scrollbars = true;
            win.Status = true;
            win.ShowModalDialog("RoleList.aspx", new string[] { "hdCheckInfo" });

        }
    }
}
